package com.linkivr.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.linkivr.dao.*;
import com.linkivr.model.*;
import com.linkivr.util.GL;

@Component("groupDao")
public class GroupDaoImpl implements GroupDao {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void add(T_group group) {
		
		sessionFactory.getCurrentSession().merge(group);
	}

	public int delete(int id) {
		
		GL.info("MANAGERDaoImpl:delete");
		return sessionFactory.getCurrentSession().createQuery("delete T_group  where group_id=:id ")
				.setInteger("id", id).executeUpdate();
		// sessionFactory.getCurrentSession().delete(manager);
	}

	public void update(T_group group) {
		sessionFactory.getCurrentSession().merge(group);
	}

	@SuppressWarnings("unchecked")
	public List<T_group> queryAll() {
		
		String hql = "from T_group ";
		GL.info(hql);
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@SuppressWarnings("unchecked")
	public List<T_group> queryAll(int start, int limit) {
		String hql = "from T_group  order by group_id desc ";
		GL.info(hql);
		return sessionFactory.getCurrentSession().createQuery(hql).setFirstResult(start).setMaxResults(limit).list();
	}

	public T_group query(int id) {
		String hql = "from T_group where  group_id=:id";
		GL.info(hql);
		return (T_group) sessionFactory.getCurrentSession().createQuery(hql).setInteger("id", id).uniqueResult();

	}

	public int queryCount() {
		Integer count = 0;
		Long lo = (Long) sessionFactory.getCurrentSession().createQuery("select count(*) from T_group ").uniqueResult();
		count = new Integer(String.valueOf(lo));
		GL.info("queryCount" + count);
		return count;
	}


}
